package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PcbaSpecialRelatedTechParamActualServiceImpl implements PcbaSpecialRelatedTechParamActualService {

    @Autowired
    private PcbaSpecialRelatedTechParamActualDao pcbaSpecialRelatedTechParamActualDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private PcbaSpecialResourceDao pcbaSpecialResourceDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    /*@Override
    public List<PcbaSpecialRelatedTechParamActual> getPcbaSpecialRelatedTechParamActualByResourceId(Integer pcbaSpecialResourceId) {
        List<PcbaSpecialRelatedTechParamActual> list = pcbaSpecialRelatedTechParamActualDao.getPcbaSpecialRelatedTechParamActualByResourceId(pcbaSpecialResourceId);
        for (PcbaSpecialRelatedTechParamActual pcbaSpecialRelatedTechParamActual : list) {
            pcbaSpecialRelatedTechParamActual.setTechDeviceParam(techDeviceParamDao.getTechDeviceParamByKey(pcbaSpecialRelatedTechParamActual.getTechDeviceParamKey()));
        }
        return list;
    }*/

    @Override
    public List<PcbaSpecialRelatedTechParamActual> getPcbaSpecialRelatedTechParamActualByResourceId(Integer pcbaSpecialResourceId) {
        List<PcbaSpecialRelatedTechParamActual> list = new ArrayList<>();
        PcbaSpecialResource deviceResource = pcbaSpecialResourceDao.getPcbaSpecialResourceById(pcbaSpecialResourceId);
        if (deviceResource != null) {
            Integer pcbaDeviceTypeId = deviceResource.getPcbaDeviceTypeId();
            List<TechDeviceParam> allTechDeviceParam = techDeviceParamDao.getAllTechDeviceParam(pcbaDeviceTypeId, null, null);
            for (TechDeviceParam techDeviceParam : allTechDeviceParam) {
                PcbaSpecialRelatedTechParamActual pcbaSpecialRelatedTechParamActual = pcbaSpecialRelatedTechParamActualDao.getPcbaSpecialRelatedTechParamActualByResourceIdAndKey(pcbaSpecialResourceId, techDeviceParam.getTechDeviceParamKey());
                if (pcbaSpecialRelatedTechParamActual != null) {
                    pcbaSpecialRelatedTechParamActual.setTechDeviceParam(techDeviceParam);
                    list.add(pcbaSpecialRelatedTechParamActual);
                } else {
                    PcbaSpecialRelatedTechParamActual p = new PcbaSpecialRelatedTechParamActual();
                    p.setTechDeviceParam(techDeviceParam);
                    p.setTechDeviceParamKey(techDeviceParam.getTechDeviceParamKey());
                    p.setTechDeviceParamValue("");
                    p.setPcbaSpecialResourceId(pcbaSpecialResourceId);
                    p.setPcbaDeviceTypeId(pcbaDeviceTypeId);
                    list.add(p);
                }
            }
        }
        return list;

//        List<PcbaSpecialRelatedTechParamActual> list = pcbaSpecialRelatedTechParamActualDao.getPcbaSpecialRelatedTechParamActualByResourceId(pcbaSpecialResourceId);
//        for (PcbaSpecialRelatedTechParamActual pcbaSpecialRelatedTechParamActual : list) {
//            pcbaSpecialRelatedTechParamActual.setTechDeviceParam(techDeviceParamDao.getTechDeviceParamByKey(pcbaSpecialRelatedTechParamActual.getTechDeviceParamKey()));
//        }
//        return list;
    }
}
